package com.starlink.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.SchoolbagGroupCodeDtoMapper;
import com.starlink.model.domain.SchoolbagGroupCodeDO;
import com.starlink.mapper.SchoolbagGroupCodeMapper;
import com.starlink.model.request.SchoolbagGroupCodeRequest;
import com.starlink.service.SchoolbagGroupCodeService;
import com.starlink.model.util.SchoolbagGroupCodeConvert;
import com.starlink.model.vo.SchoolbagGroupCodeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class SchoolbagGroupCodeServiceImpl extends ServiceImpl<SchoolbagGroupCodeMapper, SchoolbagGroupCodeDO> implements SchoolbagGroupCodeService {
    @Autowired
    private SchoolbagGroupCodeMapper schoolbagGroupCodeMapper;

    @Override
    public SchoolbagGroupCodeVO getByKey(long id) {
        SchoolbagGroupCodeVO schoolbagGroupCodeVO = SchoolbagGroupCodeDtoMapper.INSTANCE.do2VO(getById(id));
        return schoolbagGroupCodeVO;
    }

    @Override
    public PageInfo<SchoolbagGroupCodeVO> pageQuery(Pageable pageable,SchoolbagGroupCodeRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<SchoolbagGroupCodeDO> list = schoolbagGroupCodeMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<SchoolbagGroupCodeVO> schoolbagGroupCodeVOList = SchoolbagGroupCodeConvert.buildSchoolbagGroupCodeVoList(list);
        return new PageInfo(schoolbagGroupCodeVOList);
    }

    @Override
    public List<SchoolbagGroupCodeVO> listQuery(SchoolbagGroupCodeRequest request) {
        List<SchoolbagGroupCodeDO> list = schoolbagGroupCodeMapper.selectDOList(SchoolbagGroupCodeDtoMapper.INSTANCE.request2DO(request));
        List<SchoolbagGroupCodeVO> schoolbagGroupCodeVOS = SchoolbagGroupCodeConvert.buildSchoolbagGroupCodeVoList(list);
        return schoolbagGroupCodeVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(SchoolbagGroupCodeRequest request) {
        save(SchoolbagGroupCodeDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(SchoolbagGroupCodeRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(SchoolbagGroupCodeDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        schoolbagGroupCodeMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    public String getNextGroupCode(Integer type) {
        AssertUtil.assertNotNull(type, "书包分组所属的类型不能为空");
        SchoolbagGroupCodeDO one = lambdaQuery().eq(SchoolbagGroupCodeDO::getType, type).last("limit 1").one();
        AssertUtil.assertNotNull(one, "非法的书包类型");
        String fill = StrUtil.fill(one.getCurrent().toString(), '0', 10, true);
        return String.format("%s%s", one.getPrefix(), fill);
    }
}
